package com.machine.filling;

import android.app.Application;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.provider.Settings;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.WindowManager;

import com.machine.filling.db.greendao.DaoMaster;
import com.machine.filling.db.greendao.DaoSession;
import com.machine.filling.util.CloseBarUtil;
import com.machine.filling.util.CrashHandler;
import com.machine.filling.util.LogUtil;
import com.machine.filling.util.SpUtils;
import com.tencent.bugly.crashreport.CrashReport;

import java.util.ArrayList;
import java.util.List;

import okhttp3.Request;


public class MyApp extends Application {
    private static final String TAG = "MyApp";
    public static DaoMaster.DevOpenHelper mHelper;
    public static SQLiteDatabase db;
    public static DaoMaster mDaoMaster;
    public static DaoSession mDaoSession;
    private static MyApp instance;
    private static boolean isAllowDebug;
    public static MyApp getInstance() {
        return instance;
    }
    public static List<Request> errorRequests = new ArrayList<>();

    @Override
    public void onCreate() {
        super.onCreate();
        isAllowDebug = false;
        instance = this;
        LogUtil.init(true);
        setDatabase();
        initBugly();
        Log.d(TAG, "设备编号----: " + Settings.System.getString(MyApp.getInstance().getContentResolver(), Settings.Secure.ANDROID_ID));
        CloseBarUtil.closeBar();//关闭虚拟按键
        //        CloseBarUtil.showBar();//显示虚拟按键
    }

    /**
     * 初始化bugly
     */
    private void initBugly() {
        //自定义全局异常捕捉
        CrashHandler.getInstance().init(this);
        CrashReport.initCrashReport(getApplicationContext(), "ed41912b2c", false);
    }


    /**
     * 设置greenDao
     */
    public void setDatabase() {
        mHelper = new DaoMaster.DevOpenHelper(MyApp.this, "OIL_CLIENT.db", null);
        db = mHelper.getWritableDatabase();
        mDaoMaster = new DaoMaster(db);
        mDaoSession = mDaoMaster.newSession();
        //        DBUtils.init(this, Environment.getExternalStorageDirectory().getAbsolutePath()
        //                + File.separator + "oilClient/" + getPackageName(), "OIL_CLIENT.db");

    }

    public DaoSession getDaoSession() {
        return mDaoSession;
    }

    /**
     * 获取屏幕的参数，宽度和高度
     */
    private void initScreenWidth() {
        DisplayMetrics metrics = new DisplayMetrics();
        WindowManager windowManager = (WindowManager)
                this.getSystemService(Context.WINDOW_SERVICE);
        windowManager.getDefaultDisplay().getMetrics(metrics);
        int myScreenHeight = metrics.heightPixels;
        int myScreenWidth = metrics.widthPixels;
        if (myScreenHeight > myScreenWidth) {
            //平板
            SpUtils.getInstance(instance).setScreenOrientation(false);
        } else {
            //电视
            SpUtils.getInstance(instance).setScreenOrientation(true);
        }
    }
}
